package com.packer.ipc;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.ipc.ProtocolSignature;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Server;
import org.apache.hadoop.ipc.RpcEngine;

/**
 * Created by qiaqia on 2016/11/16.
 */
public class RPCServer implements MyRpcProtocal{
    Server server = null;
    public RPCServer() throws IOException, InterruptedException{
        //server = RPC.getServer(this,"localhost",8888,new Configuration());
        //相对于以前的版本有略微的改动
        RPC.Builder ins = new RPC.Builder(new Configuration());
        ins.setInstance(this);
        ins.setBindAddress("localhost");
        ins.setPort(8888);
        ins.setProtocol(MyRpcProtocal.class);
//        RPC.setProtocolEngine(new Configuration(), MyRpcProtocal.class, RpcEngine.class);
        server = ins.build();//获得一个server实例
        server.start();
        server.join();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        new RPCServer();
    }

    @Override
    public long getProtocolVersion(String protocol, long clientVersion)
            throws IOException {
        return MyRpcProtocal.versionID;
    }

    @Override
    public ProtocolSignature getProtocolSignature(String protocol,
                                                  long clientVersion, int clientMethodsHash) throws IOException {
        return new ProtocolSignature();
    }

    @Override
    public Text test(Text t) {
        if(t.toString().equals("RPC")){
            return new Text("ok");
        }
        return new Text("false");
    }
}